﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Resources;
namespace WebTeamplate
{

    public partial class Admin_ChungXe : BasePage
    {
        #region Fields
        public string urlDetail = "ChiTietChungXe.aspx";
        int count = 0;
        public Boolean allowShort = false;
        public string paramater
        {
            get
            {
                object o = ViewState["paramater"];
                if (o != null) return (string)o;
                return string.Empty;
            }
            set { ViewState["paramater"] = value; }
        }
        #endregion

        #region ViewState
        public int F_PageIndex
        {

            get
            {

                object o = ViewState["F_PageIndex"];

                if (o != null) return (int)o;

                return 0;

                //a default

            }

            set { ViewState["F_PageIndex"] = value; }

        }
        public string F_SortField
        {

            get
            {
                object o = ViewState["F_SortField"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["F_SortField"] = value; }

        }
        public string F_SortExpression
        {

            get
            {
                object o = ViewState["F_SortExpression"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["F_SortExpression"] = value; }

        }
        public int F_PageSize
        {
            get
            {
                object o = ViewState["F_PageSize"];
                if (o != null) return (int)o;
                return 0;
                //a default

            }
            set { ViewState["F_PageSize"] = value; }

        }
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
                F_txtSubName.Focus();
            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
            this.imgSubCodeSortDown.ImageUrl = CommonFunction.DEFAULT_SORTDOWN_IMAGEURL;
            this.imgSubCodeSortUp.ImageUrl = CommonFunction.DEFAULT_SORTUP_IMAGEURL;
            imgSubCodeSortDown.Click += new ImageClickEventHandler(imgSubCodeSortDown_Click);
            imgSubCodeSortUp.Click += new ImageClickEventHandler(imgSubCodeSortUp_Click);
        }
        #endregion

        #region Methods

        public void InitTextControl()
        {
            btnSearch.Text = Resources.Resource.LABEL_BUTTON_SEARCH;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            btnAddNew.Text = Resources.Resource.LABEL_BUTTON_ADDNEW;
            btnDelete.Text = Resources.Resource.LABEL_DELETE;
            this.F_SortExpression = "ASC";
            this.F_SortField = "[Order]";
        }
        private void InitData()
        {
            CommonFunction.InitStatusPublishForRadioButtonList(F_rblStatus);
            this.F_txtSubName.Text = CommonFunction.GetValueFromQueryString(this.Page, "F_txtSubName");
            this.F_rblStatus.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "F_rblStatus");
            this.F_PageIndex = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "F_PageIndex"));
            if (F_PageIndex < 1)
                F_PageIndex = 1;
            this.F_PageSize = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "F_PageSize"));
            if (F_PageSize < 1)
                F_PageSize = CommonFunction.DEFAULT_ADMIN_PAGE_SIZE;
            Pager1.PageSize = this.F_PageSize;
            F_SortExpression = CommonFunction.GetValueFromQueryString(this.Page, "F_SortExpression");
            if (F_SortExpression == string.Empty)
                F_SortExpression = "ASC";
            F_SortField = CommonFunction.GetValueFromQueryString(this.Page, "F_SortField");
            if (F_SortField == string.Empty)
                this.F_SortField = "[Order]";
        }

        public void LoadData()
        {
            paramater = this.GetPageParams(FilterFieldType.All);
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            SubCategory objCate = new SubCategory();
            objCate.ObjectSortOrder.SortExpression = this.F_SortExpression;
            objCate.ObjectSortOrder.SortField = this.F_SortField;
            objCate.ObjectPaging.PageIndex = this.F_PageIndex;
            objCate.ObjectPaging.PageSize = Pager1.PageSize;
            objCate.Condition = " where categoryId='" + CommonFunction.DEFAULT_CHUNGLOAIXE_CODE + "'";
            //objCate.Condition = "where 1=1  and categoryId='00'";
            if (F_txtSubName.Text != string.Empty)
                objCate.Condition += " and subCategoryName like N'%" + F_txtSubName.Text + "%' ";
            if (F_rblStatus.SelectedValue != string.Empty)
                objCate.Condition += " and IsPublished=" + F_rblStatus.SelectedValue + " ";
            List<SubCategoryResult> lstResult = SubCategoryUtil.SearchSubCategorySomeCondition(objCate);
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            if (this.F_SortField == "[Order]" && F_txtSubName.Text.Trim() == string.Empty && F_rblStatus.SelectedValue.ToString() == string.Empty && count > 1)
                  allowShort = true;            
            rptData.DataSource = lstResult;
            rptData.DataBind();
            Pager1.SetPaging(this.F_PageIndex, count, objCate.ToltalRow);
        }
        private void Delete(string id)
        {
            if (SubCategoryUtil.DeleteChungXe(ConvertValueObject.ParseInt(id)))
                LoadData();

        }
        public void Publish(string id)
        {
            SubCategory objCate = new SubCategory();
            objCate.SubCategoryId = ConvertValueObject.ParseInt(id);
            if (SubCategoryUtil.Publish(objCate))
                LoadData();
        }


        #endregion

        #region Events,Handler
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChungXe.aspx");
        }
        protected void drlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                SubCategoryResult cate = (SubCategoryResult)e.Item.DataItem;
                if (cate == null)
                    return;
                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();

                //ltr = (Literal)e.Item.FindControl("ltrSubCode");
                string url = urlDetail + "?" + paramater + "&subcategoryId=" + cate.SubCategoryId + "&act=" + CommonFunction.DEFAULT_COMMAND_EDIT;
                //ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategoryCode) + "</a>";
                ltr = (Literal)e.Item.FindControl("ltrSubName");
                ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategoryName) + "</a>";
                ImageButton imgButton = (ImageButton)e.Item.FindControl("btnPublish");
                imgButton.CommandArgument = cate.SubCategoryId.ToString();
                if (cate.IsPublished)
                    imgButton.ImageUrl = CommonFunction.DEFAULT_ACTIVE_IMAGEURL;
                else
                    imgButton.ImageUrl = CommonFunction.DEFAULT_DEACTIVE_IMAGEURL;
                imgButton = (ImageButton)e.Item.FindControl("imgOrderUp");
                if (e.Item.ItemIndex + 1 == count)
                    imgButton.Visible = false;
                else
                {
                    imgButton.ToolTip = Resources.Resource.TOOLTIP_ORDER_UP;
                    imgButton.CommandArgument = cate.SubCategoryId.ToString();
                    imgButton.ImageUrl = CommonFunction.DEFAULT_ORDERUP_IMAGEURL;
                }
                imgButton = (ImageButton)e.Item.FindControl("imgOrderDown");
                if (e.Item.ItemIndex == 0)
                    imgButton.Visible = false;
                else
                {
                    imgButton.CommandArgument = cate.SubCategoryId.ToString();
                    imgButton.ToolTip = Resources.Resource.TOOLTIP_ORDER_DOWN;
                    imgButton.ImageUrl = CommonFunction.DEFAULT_ORDERDOWN_IMAGEURL;
                    imgButton.Visible = true;
                }
                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = url;
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.LABEL_DELETE;
                lbtnDelete.CommandArgument = cate.SubCategoryId.ToString();
                hidListId.Value += cate.SubCategoryId + ";";
            }
        }
        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "publish":
                    {
                        Publish(id);
                        break;
                    }
                case "delete":
                    {
                        Delete(id);
                        break;
                    }
                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
            }

        }

        private void OrderDown(string id)
        {
            SubCategory obj = new SubCategory();
            obj.SubCategoryId = ConvertValueObject.ParseInt(id);
            obj.CategoryId = CommonFunction.DEFAULT_COUNTRY_CODE;
            if (SubCategoryUtil.Order(obj, CommonFunction.DEFAULT_ORDER_DOWN))
                LoadData();
        }

        private void OrderUp(string id)
        {
            SubCategory obj = new SubCategory();
            obj.SubCategoryId = ConvertValueObject.ParseInt(id);
            obj.CategoryId = CommonFunction.DEFAULT_COUNTRY_CODE;
            if (SubCategoryUtil.Order(obj, CommonFunction.DEFAULT_ORDER_UP))
                LoadData();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.F_SortExpression = "ASC";
            this.F_SortField = "[Order]";
            //reset sort order
            this.F_PageIndex = 1;
            LoadData();
        }
        void imgSubCodeSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.F_SortField = "SubCategoryName";
            this.F_SortExpression = "DESC";
            LoadData();
        }
        void imgSubCodeSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.F_SortField = "SubCategoryName";
            this.F_SortExpression = "ASC";
            LoadData();
        }

        protected void Pager1_PageIndexChanging(object obj, PagerControl.PageIndexChangeEventArgs e)
        {
            this.F_PageIndex = e.newPageIndex;
            LoadData();
        }
        protected void Pager1_PageSizeChanging(object obj, PagerControl.PageSizeChangeEventArgs e)
        {
            this.F_PageIndex = 1;
            this.F_PageSize = e.newPageSize;
            LoadData();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlDetail + "?" + paramater);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<SubCategory> lstCate = new List<SubCategory>();
            string[] arr = hidListIdSelect.Value.Split(';');
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].ToString() != string.Empty)
                {
                    SubCategory obj = new SubCategory();
                    obj.SubCategoryId = ConvertValueObject.ParseInt(arr[i].ToString());                   
                    SubCategoryUtil.DeleteChungXe(obj.SubCategoryId);
                }
            }
          
            LoadData();
        }
        #endregion
    }
}
